package com.shelpe.services.lexicon.entity;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.shelpe.services.lexicon.constants.Device;
import com.shelpe.services.lexicon.constants.Location;
import com.shelpe.services.lexicon.constants.Origin;
import com.shelpe.services.lexicon.entity.metrics.InsightMetrics;
import com.shelpe.services.lexicon.simba.InsightWordData;

import lombok.Getter;
import lombok.Setter;

@Document(collection="keyword_metrics")
@Setter
@Getter
public class KeywordMetric extends BaseEntity {

	@Id
	private String metricID;
	
	private String keywordID;
	@Indexed
	private String word;
	
	private Origin origin = Origin.taobao;//来源
	@Indexed
	private String adgroupRecordID;//推广编号，当来源为淘宝时有效
	@Indexed
	private long adgroupID;
	
	private boolean global;
	private Device device = Device.all;//设备
	private Location location = Location.all;
	@Indexed
	private String categoryID;
	@Indexed
	private String province;
	@Indexed
	private String city;
	private BigDecimal price;
	
	private Scope scope;
	private InsightMetrics metric;
	
	public void from(InsightWordData data){
		 this.metric = new InsightMetrics();
		 BeanUtils.copyProperties(data, this.metric);
		 BigDecimal bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCtr()));
		 this.metric.setCtr(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setCpc(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setRoi(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setCoverage(bd);
		 
		 this.setWord(data.getBidword());
		 this.setScope(scope);
		 
		 if(StringUtils.isNotBlank(data.getCategoryID())){
			 this.categoryID = data.getCategoryID();
		 }else if(data.getNetwork() > 0){
			 if(data.getNetwork() == InsightWordData.NETWORK_MOBILE_EXTERNAL || data.getNetwork() == InsightWordData.NETWORK_MOBILE_INTERNAL){
				 device = Device.mobile;
			 }
			 if(data.getNetwork() == InsightWordData.NETWORK_PC_EXTERNAL || data.getNetwork() == InsightWordData.NETWORK_PC_INTERNAL){
				 device = Device.pc;
			 }
		 }else{
			 this.global = true;
		 }
	}
	
}
